package com.study.service1.config;

import com.alibaba.csp.sentinel.slots.block.flow.FlowRule;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRuleManager;
import org.springframework.context.annotation.Configuration;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.List;

@Configuration
public class SentinelConfig {

    @PostConstruct
    public void doInit(){
        initSentinelRules();
    }

    // 针对方法进行流量控制
    private void initSentinelRules(){
        List<FlowRule> rules = new ArrayList<>();
        FlowRule rule = new FlowRule();
        rule.setRefResource("doSomething");
//        rule.setGrade(RuleConstant.FLOW_GRADE_QPS);
        rule.setCount(1);//每秒允许通过1个请求
        rules.add(rule);
        FlowRuleManager.loadRules(rules);
    }
}
